package com.lkxicy.wos.mapper;

import com.lkxicy.wos.pojo.entity.Holiday;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lkxicy.wos.pojo.vo.HolidayRecord;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author lkxicy
 * @since 2020-12-02
 */
public interface HolidayMapper extends BaseMapper<Holiday> {

    @Select("SELECT holiday.id,holiday.staffNumber,staff.`name`,staff.department,holiday.beginTime,holiday.endTime,holiday.holidayExplain,holiday.approval " +
            "FROM staff,holiday " +
            "WHERE staff.staffNumber=holiday.staffNumber " +
            "AND holiday.approval IS NULL ")
    public List<HolidayRecord> findUnpermitHolidayRecord();

    @Update("update holiday set approval=#{approval} where id=#{id}")
    public void permitHoliday(@Param("id") int id,@Param("approval") int approval);

    /**
     * 查看传入的时间和数据库表中的假期时间是否有冲突，有则传回该冲突的数据行，无冲突则返回null
     * @param staffNumber
     * @param beginTime
     * @param endTime
     * @return
     */
    @Select("select * from holiday where staffNumber=#{staffNumber} and " +
            "((beginTime<=#{endTime} and endTime>=#{endTime}) or " +
            "(beginTime<=#{beginTime} and endTime>=#{beginTime}) or " +
            "(beginTime>=#{beginTime} and endTime<=#{endTime}))")
    public List<Holiday> findOneHolidayRecord(@Param("staffNumber") String staffNumber,
                                              @Param("beginTime")Date beginTime,
                                              @Param("endTime") Date endTime);

    @Select("SELECT holiday.id,holiday.staffNumber,staff.`name`,staff.department,holiday.beginTime,holiday.endTime,holiday.holidayExplain,holiday.approval " +
            "FROM staff,holiday " +
            "WHERE staff.staffNumber=holiday.staffNumber " +
            "AND staff.staffNumber=#{staffNumber} " +
            "LIMIT 0,5")
    public List<HolidayRecord> searchOneHolidayRecord (@Param("staffNumber") String staffNumber);

}
